from django.contrib.admin.widgets import FilteredSelectMultiple
from django.contrib.auth.forms import ReadOnlyPasswordHashField
from django.forms import CheckboxSelectMultiple
from django_select2.forms import Select2MultipleWidget, Select2TagWidget

import xadmin
from xadmin import views
from records.models import Record
from django import forms
from .models import UserProfile


class BaseSetting(object):
    enable_themes = True
    use_bootswatch = True


class GlobalSettings(object):
    site_title = "徐氏中医"
    site_footer = "152223317@qq.com"
    menu_style = "accordion"


class UserChangeForm(forms.ModelForm):

    records = forms.ModelMultipleChoiceField(widget=CheckboxSelectMultiple,
                                             queryset=Record.objects.all(),
                                             required=False)

    class Meta:
        model = UserProfile
        fields = ('discount', 'username', 'email', 'phoneNumber', 'name', 'sex', 'dateOfBirth', 'occupation', 'address',
                  'is_staff', 'is_active', 'is_admin', 'is_superuser', 'records', 'avatar')

    def clean_password(self):
        print("asd")
        return self.initial["password"]


class UserProfileAdmin(object):
    list_display = ['name', 'username', 'phoneNumber', 'sex', 'age', 'discount', 'preDeposit', 'totalConsumption',
                    'integral', 'occupation', 'address', 'createdAt']

    form = UserChangeForm

    search_fields = ('username', 'phoneNumber', 'name')

    list_per_page = 15

    ordering = ('-createdAt',)


xadmin.site.unregister(UserProfile)
xadmin.site.register(UserProfile, UserProfileAdmin)
xadmin.site.register(views.BaseAdminView, BaseSetting)
xadmin.site.register(views.CommAdminView, GlobalSettings)
